/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.io.xml;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class DefaultProteinFieldValueFormatter
        implements ProteinFieldValueFormatter {

    public String write(String fieldName, Object fieldValue) {
        if (fieldValue==null)
            return null;

        if (fieldValue instanceof Collection) {
            return StringUtils.join((Collection)fieldValue, StaticFinalValues.SEPARATOR);
        }

        if (fieldValue instanceof Map) {
            StringBuilder sb = new StringBuilder();
            Map<Object,Object> map = (Map)fieldValue;
            for (Map.Entry entry : map.entrySet()) {
                Object key = entry.getKey();
                Object value = entry.getValue();
                sb.append(key.toString());
                sb.append(':');
                sb.append(value.toString());
                sb.append(StaticFinalValues.SEPARATOR);
            }

            if (sb.length()==0)
                return null;

            return sb.substring(0, sb.length()-StaticFinalValues.SEPARATOR.length());
        }

        return fieldValue.toString();
    }

    public Object read(String fieldName, String value) {
        return value;
    }
}
